package br.jcrsistemas.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import br.jcrsistemas.util.Configuracoes;
import br.jcrsistemas.util.Util;

public class ConexaoMySQL5 implements Conexao {

	private Connection connection;

	@Override
	public String getLastInsertID(String tabela, String campo) {
		return null;
	}

	@Override
	public Connection getConnection() {
		try {
			if (connection == null) {
				connection = DriverManager.getConnection(getURLConexao(),
						Configuracoes.getUsuarioBanco(),
						Configuracoes.getSenhaBanco());
			}
			return connection;
		} catch (SQLException e) {
			Util.trataErro(e);
			return null;
		}
	}

	@Override
	public String getDialeto() {
		return "org.hibernate.dialect.MySQL5Dialect";
	}

	@Override
	public String getURLConexao() {
		return "jdbc:mysql://" + Configuracoes.getHost() + ":"
				+ Configuracoes.getPorta() + "/" + Configuracoes.getDbName();
	}

	@Override
	public String getDriverClass() {
		return "com.mysql.jdbc.MySQLConnection";
	}

	@Override
	public void iniciaTransacao() throws SQLException {
		getConnection().setAutoCommit(false);
	}

	@Override
	public void encerraTransacao() throws SQLException {
		getConnection().setAutoCommit(true);
	}

	@Override
	public void commit() throws SQLException {
		getConnection().commit();
	}

	@Override
	public void rollback() {
		try {
			getConnection().rollback();
			Util.loga("ROLLBACK\n");
		} catch (SQLException e) {
			Util.trataErro(e);
			e.printStackTrace();
		}
	}

}
